﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace 图书管理系统
{
    class PenaltyrecordDao
    {
        public String judgeUser(String ReaderID)
        {
            String num = null;
            SqlConnection conn = new SqlConnection();
            String sql = "select count(*) from Penaltyrecord_Table where ReaderID='" + ReaderID + "'and FineState='"+"未缴费"+"'";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            try
            {
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while(reader.Read())
                {
                    num = reader[0].ToString();
                }
               
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return num;
        }
        public Boolean insert(String ReaderID,String BookNum)
        {
            SqlConnection conn = new SqlConnection();
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            String FineID = ReaderID + count().ToString();
            float FineMoney = findprice(BookNum) * findfine();
            String sql="insert into Penaltyrecord_Table(FineMoney,Generate_time,FineState,FineStyle,ReaderID,BookNUm) values('"+FineMoney+"','" +currentTime+"','"+"未缴费"+"','"+"赔书费"+"','"+ReaderID+"','" +BookNum+"')";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("生成罚款记录成功,请在个人中心缴费！");
            return true;
        }
        public int count()
        {
            SqlConnection conn = new SqlConnection();
            String sql = "Select count(*) from Penaltyrecord_Table";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            String num = null;
            while (reader.Read())
            {
                num = reader[0].ToString();
            }
            conn.Close();
            return int.Parse(num) + 1;
        }
        public float findprice(String BookNum)
        {
            SqlConnection conn = new SqlConnection();
            String sql = "Select BookPrice from Binfo_Table where BookNum='"+BookNum+"'";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            String num = null;
            while (reader.Read())
            {
                num = reader[0].ToString();
            }
            conn.Close();
            return float.Parse(num);
        }
        public float findfine()
        {
            SqlConnection conn = new SqlConnection();
            String sql = "Select  PenaltyRate from Penaltyrules_Table where FineStyle ='" + "赔书费" + "'";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            String num = null;
            while (reader.Read())
            {
                num = reader[0].ToString();
            }
            conn.Close();
            return float.Parse(num);
        }
    }
}
